package com.oyjd.framework.ui.condsel;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

/**
 * 滑动条件控件简单封装
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class CondSelectSimple extends CondSelect {

	/**
	 * 简单数据集合
	 */
	private List<CondSimpleVO> data = new ArrayList<CondSimpleVO>();

	/**
	 * 文字默认颜色
	 */
	private int foreColor = Color.BLACK;
	/**
	 * 文字选中颜色
	 */
	private int selectedColor = Color.BLACK;

	/**
	 * 构造函数
	 * 
	 * @param context
	 */
	public CondSelectSimple(Context context) {
		super(context);
		init(context);
	}

	/**
	 * 构造函数
	 * 
	 * @param context
	 * @param attrs
	 */
	public CondSelectSimple(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	/**
	 * 参数初始化
	 * 
	 * @param context
	 */
	private void init(Context context) {
		this.setOnCondListener(new OnCondListener() {

			@Override
			public void OnSelected(int pos1, int pos2) {
				refreshTxtColor();
			}

			@Override
			public void OnMoving(int pos1, int pos2) {

			}
		});
	}

	/**
	 * 创建数据视图
	 * 
	 * @param obj
	 * @return
	 */
	private View createOneCond(CondSimpleVO obj) {
		TextView tv = new TextView(this.getContext());
		tv.setGravity(Gravity.CENTER);
		tv.setTextColor(foreColor);
		tv.setText(obj.getName());
		tv.setTag(obj);
		return tv;
	}

	/**
	 * 设置简单数据
	 * 
	 * @param data
	 *            数据集合
	 */
	public void setSimpleData(List<CondSimpleVO> data) {
		this.data = data;
		List<View> tmp = new ArrayList<View>();
		for (CondSimpleVO vo : data) {
			tmp.add(createOneCond(vo));
		}
		setCondViews(tmp);
	}

	/**
	 * 获取简单数据
	 * 
	 * @param data
	 *            数据集合
	 */
	public List<CondSimpleVO> getSimpleData() {
		return data;
	}

	/**
	 * 设置文字颜色
	 * 
	 * @param color
	 */
	public void setTxtColor(int foreColor, int selectedColor) {
		this.foreColor = foreColor;
		this.selectedColor = selectedColor;
		refreshTxtColor();
	}

	/**
	 * 刷新文本颜色
	 */
	private void refreshTxtColor() {
		List<View> list = getCondViews();
		for (int i = 0; i < list.size(); i++) {
			boolean flag = mask1.pos == i || (areaFlag && mask2.pos == i);
			TextView tv = (TextView) list.get(i);
			tv.setTextColor(flag ? selectedColor : foreColor);
			tv.getPaint().setFakeBoldText(flag);
			tv.invalidate();
		}
	}

	/**
	 * 设置选项背景
	 * 
	 * @param txtBg
	 */
	public void setTxtBg(Drawable txtBg) {
		this.cond.setBackgroundDrawable(txtBg);
	}

	/**
	 * 设置选项背景
	 * 
	 * @param txtBg
	 */
	public void setTxtBg(int color) {
		this.cond.setBackgroundColor(color);
	}

	/**
	 * 设置指针图片
	 * 
	 * @param txtBg
	 */
	public void setRefBg(Drawable refBg) {
		this.mask1.setBackgroundDrawable(refBg);
		this.mask2.setBackgroundDrawable(refBg);
	}

}
